package cn.wx.scholar.core.service;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.neo4j.driver.v1.AuthTokens;
import org.neo4j.driver.v1.Driver;
import org.neo4j.driver.v1.GraphDatabase;
import org.neo4j.driver.v1.Session;
import org.springframework.stereotype.Service;

@Service
public class CsvWriteNeo4jService {
	
	private final static String csv_path = "D:/neo4j-community-3.3.2/import/data/expertFos";
	
	public void writeNeo4j() {
		
		Driver driver = GraphDatabase.driver("bolt://localhost:7687", AuthTokens.basic("neo4j", "123456"));
		Session session = driver.session();
		
		//new日期对象
		Date date = new Date();
		//转换提日期输出格式
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println("开始时间："+dateFormat.format(date));
		
		//String path = "E:/neo4j-community-3.3.2/import/data";
//		String org_path = "E:/neo4j-community-3.3.2/import/data";
//		String expert_path = "E:/neo4j-community-3.3.2/import/data/mag_papers_0/expert";
//		String paper_path = "E:/neo4j-community-3.3.2/import/data/mag_papers_0/paper";
		
		try {
			File fPath = new File(csv_path);
			
			File[] files = fPath.listFiles();
			
			for(File f : files) {
				
				System.out.println("正在执行"+f.getAbsolutePath());
				
				/*File org_file = new File(f.getAbsolutePath()+"/org");
				File[] org_files = org_file.listFiles();
				for(File fOrg : org_files) {
					String sql = "";
					sql = "load csv with headers from 'file:///e100w/"+f.getName()+"/org/"+fOrg.getName()+"' as line merge(o:org{name:line.org})";
					session.run(sql);
					System.out.println(fOrg.getName()+"执行完成");

				}*/
				
				/*File expert_file = new File(f.getAbsolutePath()+"/Author");
				File[] expert_files = expert_file.listFiles();
				for(File fExpert : expert_files) {
					String sql = "";
					sql = "load csv with headers from 'file:///e100w/"+f.getName()+"/Author/"+fExpert.getName()+"' as line merge(e:expert{name:line.Name}) with e,line match(o:org{name:line.Affiliation}) merge(o)-[r:include]->(e)"; //{year:line.year}
					session.run(sql);
					System.out.println(fExpert.getName()+"执行完成");
				}*/
				
				
//				load csv with headers from 'file:///field.csv' as line 
//				match(f:field{fId:line.fId}) ,(e:expert) where lower(e.fos) contains lower(line.name) 
//				merge(f)-[r:contain]->(e) return r
				

				//论文，专家->论文
				/*File paper_file = new File(f.getAbsolutePath()+"/Paper");
				File[] paper_files = paper_file.listFiles();
				for(File fpaper : paper_files) {
					String sql = "";
					sql = "using periodic commit 1000 load csv with headers from 'file:///e100w/"+f.getName()+"/Paper/"+ fpaper.getName() +"' as line merge(p:paper{fid:line.Id}) "
							+ "set p.fid=line.Id,p.year=line.Year,p.title=line.Title,p.doi=line.doi,p.publisher=line.publisher,p.keywords=line.Keywords,p.fos=line.fos,"
							+ "p.lang=line.lang,p.docType=line.docType";
							//+ "with p,line match(e:expert) where e.name in "
							//+ "split(replace(substring(line.authors,1,length(line.authors)-2),'\"',''),',') merge(e)-[r:publish1]->(p)";
					session.run(sql);
					System.out.println(fpaper.getName()+"执行完成");
				}*/
				
				
				/*File paper_file = new File(f.getAbsolutePath()+"/PaperAuthor");
				File[] paper_files = paper_file.listFiles();
				for(File fpaper : paper_files) {
					String sql = "";
					sql = "using periodic commit 1000 load csv with headers from 'file:///e100w/"+f.getName()+"/PaperAuthor/"+ fpaper.getName() +"' as line match(p:paper{fid:line.PaperId}) "
							+ "with p,line match(e:expert) where e.name = line.Name "
							+ " merge(e)-[r:publish1]->(p)";
					session.run(sql);
					System.out.println(fpaper.getName()+"执行完成");
				}*/
				
				File paper_file = new File(f.getAbsolutePath());
				File[] paper_files = paper_file.listFiles();
				for(File fpaper : paper_files) {
					String sql = "";
					sql = "using periodic commit 1000 load csv with headers from 'file:///expertFos/zj/"+ fpaper.getName() +"' as line match(e:expert) where ID(e)=toInt(line.expertId)"
							+ "with e,line match(f:field) where ID(f)=toInt(line.fosName) "
							+ " merge(f)-[r:containse]->(e)";
					session.run(sql);
					System.out.println(fpaper.getName()+"执行完成");
				}
				
				
			}
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
			driver.close();
			date = new Date();
			dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			System.out.println("结束时间："+dateFormat.format(date));
		}
	}
}
